<?php

require_once 'Entity.php';

define ('ORDER_STATUS_NEW', 0);
define ('ORDER_STATUS_ACCEPTED', 1);
define ('ORDER_STATUS_CANCELED', 2);
define ('ORDER_STATUS_FINISHED', 3);

/**
 * Zamówienie.
 *
 * @author Rafał Cupał
 * @author Przemysław Furmańczyk
 */
class Order extends Entity {
    protected $user = null;
    protected $restaurant = null;
    protected $date = null;
    protected $location = null;
    protected $status = null;
    protected $price = null;
    protected $profit = null;
    
    public function __construct($dataBase) {
        parent::__construct($dataBase, 'orders');
    }
    
    public function getUserId() {
        return $this->user;
    }
    
    public function setUserId($newUserId) {
        $this->user = (int)$newUserId;
    }
    
    public function getRestaurantId() {
        return $this->restaurant;
    }
    
    public function setRestaurantId($newRestaurantId) {
        $this->restaurant = (int)$newRestaurantId;
    }
    
    public function getDate() {
        return $this->date;
    }
    
    public function setDate($newDate) {
        $this->date = (int)$newDate;
    }
    
    public function getLocation() {
        return $this->location;
    }
    
    public function setLocation($newLocation) {
        $this->location = $newLocation;
    }
    
    public function getStatus() {
        return $this->status;
    }
    
    public function setStatus($newStatus) {
        $this->status = (int)$newStatus;
    }
    
    /* Cena zapisywana jest w GROSZACH! (np. cena 251 = 2,51 zł) */
    public function getPrice() {
        return $this->price;
    }
    
    public function setPrice($newPrice) {
        $this->price = $newPrice;
    }
    
    public function getProfit() {
        return $this->profit;
    }
    
    public function setProfit($newProfit) {
        $this->profit = $newProfit;
    }    
    
    public function load($rowData) {
        $this->id = $rowData['id'];
        $this->getRowData($rowData, $this->user, 'user');
        $this->getRowData($rowData, $this->restaurant, 'restaurant');
        $this->getRowData($rowData, $this->date, 'date');
        $this->getRowData($rowData, $this->location, 'location');
        $this->getRowData($rowData, $this->status, 'status');
        $this->getRowData($rowData, $this->price, 'price');
        $this->getRowData($rowData, $this->profit, 'profit');
    }
    
    public function save() {
        $rowData = array();
      
        $this->setRowData($rowData, $this->user, 'user');
        $this->setRowData($rowData, $this->restaurant, 'restaurant');
        $this->setRowData($rowData, $this->date, 'date');
        $this->setRowData($rowData, $this->location, 'location');
        $this->setRowData($rowData, $this->status, 'status');
        $this->setRowData($rowData, $this->price, 'price');
        $this->setRowData($rowData, $this->profit, 'profit');
        
        if ($this->id > 0) {
            $this->dataBase->update($this->tableName, $rowData, 
                    'id = ' . $this->id);
        }
        else {
            $this->dataBase->insert($this->tableName, $rowData);
            $this->id = $this->dataBase->lastInsertId();
        }
    }
}
